﻿/*  
    Copyright (C) <2007-2013>  <Kay Diefenthal>

    DD.UI.MediaCenter is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    DD.UI.MediaCenter is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DD.UI.MediaCenter.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Diagnostics;
using System.Reflection;
using System.Threading;
using DD.UI.MediaCenter.Logging;
using Microsoft.MediaCenter;
using Microsoft.MediaCenter.Hosting;
using Microsoft.MediaCenter.UI;

namespace DD.UI.MediaCenter.Views
{
    public class SettingsView : ModelItem
    {
        #region Private Fields

        private Boolean _isRecordingSettingsSectionVisible;
        private Boolean _isGuideSettingsSectionVisible;
        private Boolean _isTransmitterSettingsSectionVisible;
        private Boolean _isAboutSettingsSectionVisible;
        private EditableText _serverIpAddress;
        private EditableText _serverMacAddress;
        private EditableText _serverName;
        private EditableText _serverPort;
        private Choice _settingsSections;

        #endregion

        #region Properties

        public virtual Choice SettingsSections
        {
            get { return _settingsSections; }
            set
            {
                if (_settingsSections != value)
                {
                    if (_settingsSections != null)
                    {
                        _settingsSections.ChosenChanged -= OnSettingsSectionChanged;
                    }
                    _settingsSections = value;
                    if (_settingsSections != null)
                    {
                        _settingsSections.ChosenChanged += OnSettingsSectionChanged;
                    }
                    OnSettingsSectionChanged(_settingsSections, EventArgs.Empty);
                    FirePropertyChanged("SettingsSections");
                }
            }
        }

        public Boolean IsRecordingSettingsSectionVisible
        {
            get { return _isRecordingSettingsSectionVisible; }
            set
            {
                if (_isRecordingSettingsSectionVisible != value)
                {
                    _isRecordingSettingsSectionVisible = value;
                    FirePropertyChanged("IsRecordingSettingsSectionVisible");
                }
            }
        }

        public Boolean IsGuideSettingsSectionVisible
        {
            get { return _isGuideSettingsSectionVisible; }
            set
            {
                if (_isGuideSettingsSectionVisible != value)
                {
                    _isGuideSettingsSectionVisible = value;
                    FirePropertyChanged("IsGuideSettingsSectionVisible");
                }
            }
        }

        public Boolean IsTransmitterSettingsSectionVisible
        {
            get { return _isTransmitterSettingsSectionVisible; }
            set
            {
                if (_isTransmitterSettingsSectionVisible != value)
                {
                    _isTransmitterSettingsSectionVisible = value;
                    FirePropertyChanged("IsTransmitterSettingsSectionVisible");
                }
            }
        }

        public Boolean IsAboutSettingsSectionVisible
        {
            get { return _isAboutSettingsSectionVisible; }
            set
            {
                if (_isAboutSettingsSectionVisible != value)
                {
                    _isAboutSettingsSectionVisible = value;
                    FirePropertyChanged("IsAboutSettingsSectionVisible");
                }
            }
        }

        public virtual EditableText ServerName
        {
            get { return _serverName; }
            set
            {
                if (_serverName != value)
                {
                    if (_serverName != null)
                    {
                        _serverName.Submitted -= OnServerNameChanged;
                    }
                    _serverName = value;
                    if (_serverName != null)
                    {
                        _serverName.Submitted += OnServerNameChanged;
                    }
                    OnServerNameChanged(_serverName, EventArgs.Empty);
                    FirePropertyChanged("ServerName");
                }
            }
        }

        public virtual EditableText ServerIpAddress
        {
            get { return _serverIpAddress; }
            set
            {
                if (_serverIpAddress != value)
                {
                    if (_serverIpAddress != null)
                    {
                        _serverIpAddress.Submitted -= OnServerIpAddressChanged;
                    }
                    _serverIpAddress = value;
                    if (_serverIpAddress != null)
                    {
                        _serverIpAddress.Submitted += OnServerIpAddressChanged;
                    }
                    OnServerIpAddressChanged(_serverIpAddress, EventArgs.Empty);
                    FirePropertyChanged("ServerIpAddress");
                }
            }
        }

        public virtual EditableText ServerMacAddress
        {
            get { return _serverMacAddress; }
            set
            {
                if (_serverMacAddress != value)
                {
                    _serverMacAddress = value;
                    FirePropertyChanged("ServerMacAddress");
                }
            }
        }

        public virtual EditableText ServerPort
        {
            get { return _serverPort; }
            set
            {
                if (_serverPort != value)
                {
                    _serverPort = value;
                    FirePropertyChanged("ServerPort");
                }
            }
        }

        public String CompanyName
        {
            get
            {
                FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);
                return versionInfo.CompanyName;
            }
        }

        public String ProductName
        {
            get
            {
                FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);
                return versionInfo.ProductName;
            }
        }

        public String ProductVersion
        {
            get
            {
                FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);
                return versionInfo.ProductVersion;
            }
        }

        public String FileVersion
        {
            get
            {
                FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);
                return versionInfo.FileVersion;
            }
        }

        public String InternalName
        {
            get
            {
                FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);
                return versionInfo.InternalName;
            }
        }

        public String Credits
        {
            get { return String.Format("{0}{1}{2}{3}{4}", " ", "", "", "", ""); }
        }

        #endregion

        public event EventHandler SettingsSectionChanged;
        public event EventHandler ServerNameChanged;
        public event EventHandler ServerIpAddressChanged;

        protected virtual void OnSettingsSectionChanged(object sender, EventArgs args)
        {
            if (SettingsSectionChanged != null)
                SettingsSectionChanged(this, EventArgs.Empty);
        }

        protected virtual void OnServerNameChanged(object sender, EventArgs args)
        {
            if (ServerNameChanged != null)
                ServerNameChanged(this, EventArgs.Empty);
        }

        protected virtual void OnServerIpAddressChanged(object sender, EventArgs args)
        {
            if (ServerIpAddressChanged != null)
                ServerIpAddressChanged(this, EventArgs.Empty);
        }

        public Boolean ServiceCheck(String name, String ipaddress, String macaddress, String port)
        {
            Thread.Sleep(2000);
            try
            {
                return true;
            }
            catch (Exception exception)
            {
                Logger.Error("ServiceCheck :" + exception);
                return false;
            }
        }

        public void Save(String name, String ipaddress, String macaddress, String port)
        {
            try
            {
                AddInHost.Current.MediaCenterEnvironment.NavigateToPage(PageId.Start, null);
            }
            catch (Exception exception)
            {
                Logger.Error("Save :" + exception);
            }
        }
    }
}
